package cn.rui.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义线程池
 *
 * @author 徽州大都督
 * @date 2021/5/31
 */

@Configuration
@EnableAsync
public class ExecutorConfig {

    @Bean("taskExecutors")
    public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
        executor.setCorePoolSize (5);
        executor.setMaxPoolSize (20);
        executor.setQueueCapacity (Integer.MAX_VALUE);
        executor.setAwaitTerminationSeconds (10);
        executor.setKeepAliveSeconds (200);
        executor.setThreadNamePrefix ("李小瑞的多线程");
        executor.setWaitForTasksToCompleteOnShutdown (true);

        executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ());
        executor.initialize ();
        return executor;
    }
}
